/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-02-21 20:39:33
 */
import { ApiProperty } from '@nestjs/swagger';
import { BaseEntity, PaginatedResult } from 'src/common/entities/entities';
import { Status } from 'src/dictionaries/index.enum';
import { AdminUser } from 'src/modules/admin/admin-user/entities/admin-user.entity';
import {
    Entity,
    Column,
    OneToMany,
    JoinColumn,
    ManyToOne,
    Tree,
    TreeChildren,
    TreeParent,
} from 'typeorm';
import { MenuButtonEntity } from './menuButton.entity';
export enum PageType {
    'navigation' = 1,
    'page' = 2,
}
export const MenuFieldName = {
    name: '菜单名称',
    path: '路径',
    route: '路由别名',
    sort: '排序',
    type: '页面类型',
    is_use: '是否可用',
    is_navbar: '是否在导航栏显示',
    description: '描述',
    createById: '创建人',
    menuButton: '按钮ID',
}
@Entity({ name: 'menu' })
@Tree('closure-table') // 使用闭包表来实现递归关系
export class MenuEntity extends BaseEntity {
    /** 菜单名称 */
    @ApiProperty({ example: 'name', description: '菜单名称' })
    @Column({ type: 'varchar', comment: '菜单名称', length: 255 })
    name: string;
    /** 路径 */
    @ApiProperty({ example: 'path', description: '路径' })
    @Column({ type: 'varchar', comment: '路径', length: 255 })
    path: string;

    /** 路由别名 */
    @ApiProperty({ example: 'route', description: '路由别名' })
    @Column({ type: 'varchar', comment: '路由别名', length: 255 })
    route: string;

    /** 排序 */
    @ApiProperty({ example: 'sort', description: '排序' })
    @Column({ type: 'int', comment: '排序', default: 0 })
    sort: number;

    /** 页面类型 */
    @ApiProperty({
        enum: PageType,
        example: 'type',
        description: '页面类型',
    })
    @Column({
        type: 'enum',
        enum: PageType,
        default: PageType.navigation,
    })
    type: PageType;

    /** 是否可用,2:否；1:是 */
    @ApiProperty({ example: 'is_use', description: '是否可用,2:否；1:是' })
    @Column({
        comment: '是否可用,2:否；1:是',
        type: 'enum',
        default: 1,
        enum: [1, 2],
    })
    is_use: string;

    /** 是否在导航栏显示,2:否；1:是 */
    @ApiProperty({ example: 'is_navbar', description: '是否在导航栏显示,2:否；1:是' })
    @Column({
        comment: '是否在导航栏显示,2:否；1:是',
        type: 'enum',
        default: 1,
        enum: [1, 2],
    })
    is_navbar: string;

    /** 描述 */
    @ApiProperty({ example: 'description', description: '描述' })
    @Column({ type: 'varchar', comment: '描述' })
    description: string;

    @TreeChildren() // 子分类
    children: MenuEntity[];
    @TreeParent() // 父分类
    parent: MenuEntity;
    /** 创建人 */
    @ApiProperty({ example: 'createById', description: '创建人' })
    @Column({ type: 'int', comment: '创建人' })
    createById: number;
    @ManyToOne(() => AdminUser, (adminUser) => adminUser.rolesCreateBy) // 指定另一面作为第二个参数
    @JoinColumn({ name: 'createById' })
    createBy: AdminUser;
    // 按钮ID
    @OneToMany(() => MenuButtonEntity, (button) => button.menu)
    menuButton: MenuButtonEntity[];
}
export class MenuResult extends PaginatedResult {
    @ApiProperty({ example: 'data', description: 'data' })
    data: MenuEntity;
}
